<?php
    /**
     * Created by PhpStorm.
     * User: huangzhongwei
     * Date: 2018/10/25
     * Time: 18:21
     */

    namespace app\common\model;


    use think\Model;

    class Order extends Model
    {
        protected $table = "order";
        protected $autoWriteTimestamp = true;


        //获取跑男所有订单

        public static function getAllOrders($page = 1, $size = 20){
            $result = self::with('goods,weight,pickUpGoods,harvestAddress,pickUpAddress,runningMan,createOrder,University')
                          ->order('create_time', 'desc')
                          ->paginate($size, false, ['page' => $page]);
            return $result;
        }


        //获取跑男端首页订单  可接订单
        public static function getCanOrderable($university_id, $page = 1, $size = 20)
        {
            $result = self::with('goods,weight,pickUpGoods,harvestAddress,pickUpAddress,runningMan,createOrder')
                          ->where(['university_id' => $university_id, 'state' => 2])
                          ->paginate($size, true, ['page' => $page]);

            return $result;
        }

        //获取跑男端未完成订单
        public static function incompleteOrders($uid, $page = 1, $size = 20)
        {
            $result = self::where(['state' => [2, 3, 4], 'order_account_id' => $uid])
                          ->with('goods,weight,pickUpGoods,harvestAddress,pickUpAddress,runningMan,createOrder')
                          ->order('order_time', 'desc')
                          ->paginate($size, true, ['page' => $page]);
            return $result;
        }

        //获取跑男端已完成订单
        public static function completedOrder($uid, $page = 1, $size = 20)
        {
            $result = self::where(['state' => 5, 'order_account_id' => $uid])
                          ->with('goods,weight,pickUpGoods,harvestAddress,pickUpAddress,runningMan,createOrder')
                          ->order('order_time', 'desc')
                          ->paginate($size, true, ['page' => $page]);
            return $result;
        }

        //获取用户端进行中订单

        public static function onGoingOrder($uid, $page = 1, $size = 20)
        {
            $result = self::where(['state' => [1, 2, 3, 4,], 'account_id' => $uid])
                          ->with('goods,weight,pickUpGoods,harvestAddress,pickUpAddress,createOrder')
                          ->order('create_time', 'desc')
                          ->paginate($size, true, ['page' => $page]);
            return $result;
        }

        //获取用户已结束订单
        public static function endedOrder($uid, $page = 1, $size = 20)
        {
            $result = self::where(['state' => [5, 6,7,8], 'account_id' => $uid])
                          ->with('goods,weight,pickUpGoods,harvestAddress,pickUpAddress,runningMan,createOrder')
                          ->order('create_time', 'desc')
                          ->paginate($size, true, ['page' => $page]);
            return $result;
        }

        public function goods()
        {
            return $this->belongsToMany('goods', 'order_goods', 'goods_id', 'order_id');
        }

        public function weight()
        {
            return $this->belongsTo('Weight', 'weight_id');
        }

        public function pickUpGoods()
        {
            return $this->belongsTo('PickUpGoods', 'pick_up_goods_id');
        }

        public function orderGoods(){
            return $this->hasMany('OrderGoods','order_id');
        }

        //关联收货地址

        public function harvestAddress(){
            return $this->belongsTo('HarvestAddress','harvest_address_id');
        }

        //关联取货地址
        public function pickUpAddress(){
            return $this->belongsTo('HarvestAddress','pick_up_address_id');
        }
        //关联跑男
        public function runningMan(){
            return $this->belongsTo('Account','order_account_id');
        }
        //关联订单创建人
        public function createOrder(){
            return $this->belongsTo('Account','account_id');
        }

        //关联大学

        public function university(){
            return $this->belongsTo('University','university_id');
        }




    }